package com.mingmen.canting.adapter;


import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.mingmen.canting.R;
import com.mingmen.canting.defineview.GlideRoundTransform;
import com.mingmen.canting.httpconfig.AppConfig;
import com.mingmen.canting.model.CaiGouListModel;
import com.mingmen.canting.model.CaiGouRightModel;

import java.util.List;


/**
 * @author qdafengzi
 */
public class CaigouZhongiRightAdapter extends RecyclerView.Adapter<CaigouZhongiRightAdapter.ViewHolder> {
    private Context mContext;
    private List<CaiGouRightModel.DetailsBean> mList;

    private int mPosition;

    //定义一个点击事件的接口
    public interface OnitemClick {
        void onItemClick(View view, int position, List<CaiGouRightModel.DetailsBean> mList);
    }

    private OnitemClick onitemClick;   //定义点击事件接口

    public void setOnitemClickLintener(OnitemClick onitemClick) {
        this.onitemClick = onitemClick;
    }

    public CaigouZhongiRightAdapter(Context context, List<CaiGouRightModel.DetailsBean> list, int position) {
        mContext = context;
        mList = list;
        mPosition = position;
    }

    /**
     * 新增方法
     *
     * @param position 位置
     */
    public void setPosition(int position) {
        this.mPosition = position;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_caigouzhong_right, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        CaiGouRightModel.DetailsBean detailsBean = mList.get(position);
        holder.commodityName.setText(detailsBean.getCommodityName());
        holder.amount.setText("计划数量:"+detailsBean.getAmount());
        holder.spec.setText("规格:"+detailsBean.getSpecs() + "");
        holder.price.setText(mList.get(position).getPrice() + "");
        Glide.with(mContext).load(AppConfig.IMGURL + detailsBean.getPicture()).dontAnimate().placeholder(R.mipmap.zhanwei).error(R.mipmap.zhanwei).transform(new GlideRoundTransform(mContext, 5)).into(holder.ivAvatar);
//        holder.applyAmount.setText("实发数量: " + detailsBean.getApplyAmount());
        holder.applyAmount.setText("实发数量: " + "一");
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }


    class ViewHolder extends RecyclerView.ViewHolder {

        TextView commodityName;
        TextView amount;

        TextView price;
        ImageView ivAvatar;
        TextView spec;
        TextView applyAmount;

        ViewHolder(View view) {
            super(view);

            commodityName = view.findViewById(R.id.commodityName);
            amount = view.findViewById(R.id.amount);
            spec = view.findViewById(R.id.spec);
            price = view.findViewById(R.id.price);
            ivAvatar = view.findViewById(R.id.ivAvatar);
            applyAmount = view.findViewById(R.id.applyAmount);
            //这里设置一个tag,作为被嵌套的recycleview item点击事件的 position
         /*   lay_option.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mList.get((int) v.getTag()).select = true;

                }
            });*/


        }
    }
}